package cn.tedu.blog.server.controller.api;

import cn.tedu.blog.server.pojo.dto.ArticleHistoryDTO;
import cn.tedu.blog.server.pojo.vo.ArticleHistoryVO;
import cn.tedu.blog.server.security.LoginPrincipal;
import cn.tedu.blog.server.service.IArticleHistoryService;
import cn.tedu.blog.server.utils.JsonPage;
import cn.tedu.blog.server.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@Slf4j
@RestController
@RequestMapping("/history")
@Api(tags = "文章浏览历史模块")
public class ArticleHistoryController {
    @Autowired
    private IArticleHistoryService articleHistoryService;
    
    @GetMapping("/insert")
    @ApiOperation("新增浏览历史")
    public JsonResult insertHistory(@AuthenticationPrincipal LoginPrincipal loginPrincipal,
                                    ArticleHistoryDTO articleHistoryDTO) {
        Long userId = loginPrincipal.getId();
        articleHistoryDTO.setUserId(userId);
        articleHistoryService.insertHistory(articleHistoryDTO);
        return JsonResult.ok();
    }
    


    @GetMapping("/count")
    @ApiOperation("根据userId查询浏览历史数量")
    public JsonResult countHistory(@AuthenticationPrincipal LoginPrincipal loginPrincipal) {
        Long userId = loginPrincipal.getId();
        int rows = articleHistoryService.countHistory(userId);
        return JsonResult.ok(rows);
    }

    // 根据userId和页面展示条数查询历史浏览
    @GetMapping("/list")
    @ApiOperation("根据userId和页面展示条数查询历史浏览")
    public JsonResult listArticleHistory(@AuthenticationPrincipal LoginPrincipal loginPrincipal,
                                         Integer pageSize) {

        log.error("传过来的分页数量：{}",pageSize);
        Long userId = loginPrincipal.getId();
        List<ArticleHistoryVO> list = articleHistoryService.listArticleHistory(userId, pageSize);
        return JsonResult.ok(list);
    }
}
